package com.ruoyi.statisticalCenter.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.utils.GetDateStringUtils;
import com.ruoyi.hqfinance.domain.HqNetworkBillDetail;
import com.ruoyi.hqfinance.mapper.HqNetworkBillDetailMapper;
import com.ruoyi.logistic.domain.ScHqIncomeAndExpendCollectDetail;
import com.ruoyi.logistic.mapper.ScHqIncomeAndExpendCollectDetailMapper;
import com.ruoyi.statisticalCenter.domain.ScHqIncomeAndExpendCollect;
import com.ruoyi.statisticalCenter.domain.vo.DateStrVo;
import com.ruoyi.statisticalCenter.domain.vo.ScHqIncomeAndExpendCollectExportVo;
import com.ruoyi.statisticalCenter.domain.vo.ScHqIncomeAndExpendCollectPointVo;
import com.ruoyi.statisticalCenter.domain.vo.ScHqIncomeAndExpendCollectVo;
import com.ruoyi.statisticalCenter.mapper.ScHqIncomeAndExpendCollectMapper;
import com.ruoyi.statisticalCenter.service.ScHqIncomeAndExpendCollectService;
import io.swagger.annotations.ApiModelProperty;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ScHqIncomeAndExpendCollectServiceImpl implements ScHqIncomeAndExpendCollectService {
    @Autowired
    private ScHqIncomeAndExpendCollectMapper scHqIncomeAndExpendCollectMapper;
    @Autowired
    private HqNetworkBillDetailMapper hqNetworkBillDetailMapper;
    @Autowired
    private ScHqIncomeAndExpendCollectDetailMapper hqIncomeAndExpendCollectDetailMapper;
    @Override
    public List<ScHqIncomeAndExpendCollect> collect(ScHqIncomeAndExpendCollectVo vo) {
        PageHelper.startPage(vo.getPageDate());
        List<ScHqIncomeAndExpendCollect> collects = hqIncomeAndExpendCollectDetailMapper.selectScHqIncomeAndExpendCollect(vo);
        if (collects==null || collects.size() ==0){
            PageHelper.startPage(vo.getPageDate());
            collects = scHqIncomeAndExpendCollectMapper.collect(vo);
        }
        
        assert collects != null;
        for (ScHqIncomeAndExpendCollect collect : collects){
            collect.setCollectDate(vo.getStartTime() + "至" + vo.getEndTime());
        }
        return collects;
    }

    @Override
    public List<ScHqIncomeAndExpendCollect> collectExport(ScHqIncomeAndExpendCollectExportVo vo  , OutputStream outputStream) {
        ScHqIncomeAndExpendCollectVo collectVo = new ScHqIncomeAndExpendCollectVo();
        BeanUtils.copyProperties(vo,collectVo);
        List<ScHqIncomeAndExpendCollect> collects = hqIncomeAndExpendCollectDetailMapper.selectScHqIncomeAndExpendCollect(collectVo);
        if (collects==null || collects.size()==0){
            collects = scHqIncomeAndExpendCollectMapper.collectExport(vo);
        }
        assert collects != null;
        for (ScHqIncomeAndExpendCollect collect : collects){
            collect.setCollectDate(vo.getStartTime() + "至" + vo.getEndTime());
        }
        Workbook workbook = new XSSFWorkbook(); // 创建一个新的Excel工作簿
        Sheet sheet = workbook.createSheet("Amount Data"); // 创建一个新的工作表
        DecimalFormat df = new DecimalFormat("0.00");

        // 创建边框样式
        CellStyle borderStyle = workbook.createCellStyle();
        BorderStyle thin = BorderStyle.THIN;
        short blackColorIndex = IndexedColors.BLACK.getIndex();
        borderStyle.setBorderTop(thin);
        borderStyle.setBorderBottom(thin);
        borderStyle.setBorderLeft(thin);
        borderStyle.setBorderRight(thin);
        borderStyle.setTopBorderColor(blackColorIndex);
        borderStyle.setBottomBorderColor(blackColorIndex);
        borderStyle.setLeftBorderColor(blackColorIndex);
        borderStyle.setRightBorderColor(blackColorIndex);

// 创建表头样式
        CellStyle headerStyle = workbook.createCellStyle();
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setFontHeightInPoints((short) 14); // 设置字体大小
        headerStyle.setFont(headerFont);
        headerStyle.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中
        headerStyle.setBorderTop(thin);
        headerStyle.setBorderBottom(thin);
        headerStyle.setBorderLeft(thin);
        headerStyle.setBorderRight(thin);
        headerStyle.setTopBorderColor(blackColorIndex);
        headerStyle.setBottomBorderColor(blackColorIndex);
        headerStyle.setLeftBorderColor(blackColorIndex);
        headerStyle.setRightBorderColor(blackColorIndex);
        
        Row headerRow = sheet.createRow(0); // 创建表头行
        String[] headers = {"统计日期","名称","费用大类","费用小类","费用名称","收入","支出","合计金额"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
            
        }
        
        int rowNum = 1; // 行索引从1开始（因为已经有表头行）

        // 将数据写入Excel表格
        for (ScHqIncomeAndExpendCollect collect : collects) {
            Row dataRow = sheet.createRow(rowNum++);
            Cell cell0 = dataRow.createCell(0);
            cell0.setCellValue(collect.getCollectDate());
            cell0.setCellStyle(borderStyle);
            Cell cell1 = dataRow.createCell(1);
            cell1.setCellValue(collect.getNetworkName());
            cell1.setCellStyle(borderStyle);
            Cell cell2 = dataRow.createCell(2);
            cell2.setCellValue(collect.getFeeBigKind());
            cell2.setCellStyle(borderStyle);
            Cell cell3 = dataRow.createCell(3);
            cell3.setCellValue(collect.getFeeSmallKind());
            cell3.setCellStyle(borderStyle);
            Cell cell4 = dataRow.createCell(4);
            cell4.setCellValue(collect.getFeeName());
            cell4.setCellStyle(borderStyle);
            Cell cell5 = dataRow.createCell(5);
            cell5.setCellStyle(borderStyle);
            if (collect.getIncome() != null){
                cell5.setCellValue(df.format(collect.getIncome().doubleValue()));
            }
            Cell cell6 = dataRow.createCell(6);
            cell6.setCellStyle(borderStyle);
            if (collect.getExpend() != null){
                cell6.setCellValue(df.format(collect.getExpend().doubleValue()));
            }
            Cell cell7 = dataRow.createCell(7);
            cell7.setCellStyle(borderStyle);
            if (collect.getTotalAmount() != null){
                cell7.setCellValue(df.format(collect.getTotalAmount().doubleValue()));
            }
        }
        try {
            workbook.write(outputStream); // 将工作簿写入输出流
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            workbook.close(); // 关闭工作簿
        } catch (IOException e) {
            e.printStackTrace();
        }
        return collects;
    }


    @Override
    public List<HqNetworkBillDetail> point(ScHqIncomeAndExpendCollectPointVo vo) {
        PageHelper.startPage(vo.getPageDate());
        return hqNetworkBillDetailMapper.hqIncomeAndExpendPoint(vo);
    }

    @Override
    public R cache(DateStrVo dateStrVo) {
        //网店承包区
//        Integer[] networkLevels = {0, 1, 2};
        String[] costBigKindIds= {"奖惩","面单","派费","中转","总部市场","综合","运输"};
        String[] policySmallKinds= {"遗失、延误、破损","时效类考核","车辆类考核","平台工单","质量考核","扶持补助",
                "政策返还","一口价","面单费","航海项目","车线补贴","大货计重","周期性派费","补贴派费","基础派费","乡镇补贴","星计划","物料派费","特价业务"
                ,"按需派费","其他派费","签收及综合考核","退件费","操作费","短驳费","中转费","整车称重","全网出港费","总部市场","违规行为考核","综合类","带回货","航空代理提货"
                ,"劳务费","汽运线路收费"};
        List<String> days = GetDateStringUtils.getDateStr(dateStrVo.getStartDateStr(), dateStrVo.getEndDateStr());
        List<String> dateStrList = hqIncomeAndExpendCollectDetailMapper.selectllDateStr();
        days.removeAll(dateStrList);
        for (String day : days) {
            String time = day;
        
            ScHqIncomeAndExpendCollectVo scHqIncomeAndExpendCollectVo = new ScHqIncomeAndExpendCollectVo();
            scHqIncomeAndExpendCollectVo.setEndTime(time);
            scHqIncomeAndExpendCollectVo.setStartTime(time);
    //        for (Integer networkLevel : networkLevels) {
                for (String costBigKindId : costBigKindIds) {
                    for (String policySmallKind : policySmallKinds) {
                        scHqIncomeAndExpendCollectVo.setPolicySmallKind(policySmallKind);
                        scHqIncomeAndExpendCollectVo.setPolicyBigKind(costBigKindId);
                        List<ScHqIncomeAndExpendCollect> collect = scHqIncomeAndExpendCollectMapper.collect(scHqIncomeAndExpendCollectVo);
                        for (ScHqIncomeAndExpendCollect scHqIncomeAndExpendCollect : collect) {
                            ScHqIncomeAndExpendCollectDetail scHqIncomeAndExpendCollectDetail = new ScHqIncomeAndExpendCollectDetail();
                            BeanUtils.copyProperties(scHqIncomeAndExpendCollect,scHqIncomeAndExpendCollectDetail);
                            scHqIncomeAndExpendCollectDetail.setPolicySmallKind(policySmallKind);
                            scHqIncomeAndExpendCollectDetail.setPolicyBigKind(costBigKindId);
                            scHqIncomeAndExpendCollectDetail.setCollectDate(time);
                            hqIncomeAndExpendCollectDetailMapper.insert(scHqIncomeAndExpendCollectDetail);
                        }
                    }
                }
            System.out.println(time+"导入成功");
    
    //        }
        }
        return R.ok("导入成功");
    }

}
